package com.elite;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class RsvSms extends HttpServlet{
	private static final Log log=LogFactory.getLog(RsvSms.class);
	private String ip;
	private static String hexString= "0123456789ABCDEF";
	public void init(){
		log.info("-------------RsvSms Start----------------");
		ip=this.getInitParameter("IP");		
		log.info("ip:"+ip);
	}
	public static void main(String[] args){
//		try {
//			String a=encode("中文");
//			System.out.println(decode(a));
//		} catch (UnsupportedEncodingException e) {
//			// TODO Auto-generated catch block
//			e.printStackTrace();
//		}
		System.out.print(toStringHex("b2e2cad4d0c5cfa231"));
	}
	public static String toStringHex(String s){
		byte[] baKeyword = new byte[s.length()/2]; 
		for(int i=0;i<baKeyword.length;i++){
			try{
				baKeyword[i]=(byte)(0xff&Integer.parseInt(s.substring(i*2,i*2+2),16));
			}catch(Exception e){
				log.info("baKeyword[i]:"+e.toString());
			}
		}
		try{
			s=new String(baKeyword,"gbk");
		}catch(Exception e){
			log.info("s=new String:"+e.toString());
		}
		return s;
	}
	public static String decode(String bytes) throws UnsupportedEncodingException{
		ByteArrayOutputStream baos=new ByteArrayOutputStream(bytes.length()/2);
		for(int i=0;i<bytes.length();i+=2){
			baos.write((hexString.indexOf(bytes.charAt(i))<<4|hexString.indexOf(bytes.charAt(i+1)))); 
		}
		return new String(baos.toByteArray(),"gbk");
	}
	public static String encode(String str){
		byte[] bytes=str.getBytes();
		StringBuilder sb=new StringBuilder(bytes.length*2);
		for(int i=0;i<bytes.length;i++){
			sb.append(hexString.charAt((bytes[i]&0xf0)>>4)); 
			sb.append(hexString.charAt((bytes[i]&0x0f)>>0));
		}
		return sb.toString();
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {			
//		String c="c";
//		 tony tony=new tony();
//		  request.setAttribute("tony", tony);
//		  request.setAttribute("c", c);
//		 tony aaa=(tony)request.getAttribute("tony");
//		 //aaa.setA("a1");
//		 tony1 tony1=new tony1();
//		 tony1.setB("b1");
//		 aaa.setTony1(tony1);
//		 c="c1";
//		 System.out.println(((tony)request.getAttribute("tony")).getA());
//		 System.out.println(((tony)request.getAttribute("tony")).getTony1().getB());
//		 System.out.println(request.getAttribute("c"));
		

		
		log.info("---------------------------------------RsvSms---------------------------------------------------");
		request.setCharacterEncoding("UTF-8");
        ServletContext application;   
        WebApplicationContext wac;   
        application = getServletContext();   
        wac = WebApplicationContextUtils.getWebApplicationContext(application);//获取spring的context  
        com.elite.FtpBean ftpbean = (com.elite.FtpBean) wac.getBean("ftpbean");  
    
        //广联达       
		String mobile = Util.filter(request.getParameter("mobile"));
		String content = Util.filter(request.getParameter("content"));
		String xh = Util.filter(request.getParameter("xh"));
		String time = Util.filter(request.getParameter("time"));
		String urljudge=ftpbean.test1("dbo.Glodon_P_SmsIN",content,mobile,xh);    //广联达
		
        //中航技
//		String mobile = Util.filter(request.getParameter("sa"));
//		String content = Util.filter(request.getParameter("sm"));
//		content=toStringHex(content);
//		String xh = Util.filter(request.getParameter("da"));
//		String spid = Util.filter(request.getParameter("spid"));
//		String momsgid = Util.filter(request.getParameter("momsgid"));
//		log.info("sa:"+mobile);
//		log.info("sm:"+content);
//		log.info("da:"+xh);
//		log.info("spid:"+spid);
//		log.info("momsgid:"+momsgid);
//		
//		String url1="command=MO_RESPONSE&spid="+spid+"&momsgid="+momsgid+"&mostat=ACCEPT&moerrcode=000";
//		log.info("响应url:"+url1);
//		
//		String urljudge=ftpbean.test1("Glodon_P_SmsIN",content,mobile,xh); 		
//		log.info("调用Glodon_P_SmsIN结果:"+urljudge); 
//		   
//		
//		ByteArrayOutputStream bout=new ByteArrayOutputStream();
//		bout.write(url1.getBytes("UTF-8"));
//		log.info("bout.size():"+bout.size());
//		response.setContentLength(bout.size());
//		bout.writeTo(response.getOutputStream());
//		bout.flush();
//		bout.close();

		   
//		   PrintWriter out = response.getWriter(); 
//		   out.println(url1);
//		   out.flush(); out.close();
		
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
	throws ServletException, IOException{
		doGet(request,response);
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the Post method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}
}
